package org.ttitfly.ttcore.util;
import java.lang.reflect.Field;
import java.util.Date;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springside.modules.utils.reflection.ReflectionUtils;
import org.ttitfly.ttcore.entity.IdEntity;


public class ObjectConverter {
	private static final Log logger = LogFactory.getLog(ObjectConverter.class);
	
	public static final String[] ignoreFields = {"serialVersionUID","id","processInstanceId"};
	
	/**
	 * 判断是否是基本数据类型
	 * @param clazz
	 * @return
	 */
	private static boolean baseDataType(Class<?> clazz){
		boolean isBaseDataType = false;
		if (Boolean.class == clazz) {
			isBaseDataType = true;
        } else if (Long.class == clazz) {
        	isBaseDataType = true;
        } else if (Integer.class == clazz) {
        	isBaseDataType = true;
        } else if (Double.class == clazz) {
        	isBaseDataType = true;
        } else if (Float.class == clazz) {
        	isBaseDataType = true;
        } else if (Date.class == clazz) {
        	isBaseDataType = true;
        } else if (String.class == clazz) {
        	isBaseDataType = true;
        } else if (int.class == clazz) {
        	isBaseDataType = true;
        } else if (long.class == clazz) {
        	isBaseDataType = true;
        } else if (float.class == clazz) {
        	isBaseDataType = true;
        } else if (Boolean.class == clazz) {
        	isBaseDataType = true;
        } else if (boolean.class == clazz) {
        	isBaseDataType = true;
        } else if (double.class == clazz) {
        	isBaseDataType = true;
        } 
		return isBaseDataType;
		
	}
	/**
	 * 对象值的拷贝。（只限制基本数据类型的拷贝，非基本数据类型拷贝ID）
	 * @param sourceObj
	 * @param targetObj
	 * @param clazz
	 */
	@SuppressWarnings("unchecked")
	public static void convertCopy(Object sourceObj,Object targetObj,Class clazz ){
		try{
			Field[] fields = clazz.getDeclaredFields();
			for(Field field : fields){
				String fieldName = field.getName();
				boolean isIgnore = false;;
				for(String ignoreField : ignoreFields){
					if(ignoreField.equalsIgnoreCase(fieldName)){
						isIgnore = true;
						break;
					}
				}
				if(isIgnore){
					continue;
				}
				Class fieldType = field.getType();
				Class<?> propertyClazz = PropertyUtils.getPropertyType(sourceObj, fieldName);
				Object sourcePropertyObjValue = ReflectionUtils.forceGetProperty(sourceObj,fieldName);
				
				boolean isBaseDataType = baseDataType(propertyClazz);
				if(isBaseDataType){//基本数据类型属性赋值
					//ReflectionUtils.forceSetProperty(targetObj, fieldName, sourcePropertyObjValue);
					ReflectionUtils.invokeSetterMethod(targetObj, fieldName, sourcePropertyObjValue);
				}else{//非基本数据类型强制赋值ID
					if(sourcePropertyObjValue != null && (sourcePropertyObjValue instanceof IdEntity)){
						Object idValue = ReflectionUtils.forceGetProperty(sourcePropertyObjValue,"id");
						if(idValue != null){
							//ReflectionUtils.forceSetProperty(targetObj, fieldName, null);
							ReflectionUtils.invokeSetterMethod(targetObj, fieldName, null);
							Object targetPropertyObjValue = fieldType.newInstance();//目标对象的属性对象
							//ReflectionUtils.forceSetProperty(targetPropertyObjValue, "id", idValue);
							ReflectionUtils.invokeSetterMethod(targetPropertyObjValue, "id", idValue);
							//ReflectionUtils.forceSetProperty(targetObj, fieldName, targetPropertyObjValue);
							ReflectionUtils.invokeSetterMethod(targetObj, fieldName, targetPropertyObjValue);
						}
					}
				}
				
			}
		}catch(Exception e){
			logger.error("data converter error!");
		}
		
	}
	
}
	
